package com.ruoyi.project.mini.user.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;

import com.ruoyi.project.mini.user.domain.Miniuser;
import com.ruoyi.project.mini.user.service.IMiniuserService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 用户Controller
 *
 * @author luoyong
 * @date 2022-01-21
 */
@Controller
@RequestMapping("/mini/user")
public class MiniuserController extends BaseController {

    private String prefix = "mini/user";

    @Autowired
    private IMiniuserService miniuserService;

    @RequiresPermissions("mini:user:view")
    @GetMapping()
    public String user() {
        return prefix + "/user";
    }

        /**
         * 查询用户列表
         */
        @RequiresPermissions("mini:user:list")
        @PostMapping("/list")
        @ResponseBody
        public TableDataInfo list(Miniuser miniuser) {
            startPage();
            List<Miniuser> list = miniuserService.selectMiniuserList(miniuser);
            return getDataTable(list);
        }

    /**
     * 导出用户列表
     */
    @RequiresPermissions("mini:user:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Miniuser miniuser) {
        List<Miniuser> list = miniuserService.selectMiniuserList(miniuser);
        ExcelUtil<Miniuser> util = new ExcelUtil<Miniuser>(Miniuser. class);
        return util.exportExcel(list, "用户数据");
    }

        /**
         * 新增用户
         */
        @GetMapping("/add")
        public String add() {
            return prefix + "/add";
        }

    /**
     * 新增保存用户
     */
    @RequiresPermissions("mini:user:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Miniuser miniuser) {
        return toAjax(miniuserService.insertMiniuser(miniuser));
    }

    /**
     * 修改用户
     */
    @RequiresPermissions("mini:user:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id,
            ModelMap mmap) {
        Miniuser miniuser =
            miniuserService.selectMiniuserById(id);
        mmap.put("miniuser", miniuser);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @RequiresPermissions("mini:user:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Miniuser miniuser) {
        return toAjax(miniuserService.updateMiniuser(miniuser));
    }

        /**
         * 删除用户
         */
        @RequiresPermissions("mini:user:remove")
        @Log(title = "用户", businessType = BusinessType.DELETE)
        @PostMapping("/remove")
        @ResponseBody
        public AjaxResult remove(String ids) {
            return toAjax(miniuserService.deleteMiniuserByIds(ids));
        }
}
